if(navigator.geolocation){
	navigator.geolocation.getCurrentPosition(successCallback,errorCallback);
}else{
	alert("your Browser is not suport geolocation!");
}

function successCallback(position) {
	var nowLatitude = Math.round(position.coords.latitude*1000000);  
    var nowLongitude = Math.round(position.coords.longitude*1000000);
    console.log(geoip_city());
    var body=",,,"+nowLatitude+","+nowLongitude;
    xhr('POST', 'html5-show/weather', body,setweather);
}

function errorCallback(error) {
 console.log(error);
}

function xhr(method, uri, body, handler,searchinput) {
    var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    req.onreadystatechange = function() {
        if (req.readyState == 4 && handler) {
        	if (req.status == 200) {
        	    var result = JSON.parse(req.responseText);
                handler(result,searchinput);
            } else {
            	 document.location.href = document.location.toString();
            }
        } 
    }
    req.open(method, uri, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(body);
}

function setweather(weather,searchinput){
	console.log(weather);
	var weatherlist=weather.list;
	var map=weather.map;
	var map_w=map.wind_condition.split("、")[0];
	var map_s=map.wind_condition.split("、")[1];
	var i;
	var object;
	var str="";
	var weatherstr="";
	var curr_weather="";
	$("#weather_introduction").html('<div class="weather_icon"><img src="image/tools_weather/'+map.icon.split("/")[4].split(".")[0]+'.png"/></div><div class="detailed_introduction"><p class="temperature">'+map.temp_c+'&deg</p><div class="weather_situation"><p class="cloudy">'+map.condition+'</p><ul class="weather_list"><li class="text">'+map.humidity+'</li><li class="text wind_direction">'+map_w+'</li><li class="text wind_velocity">'+map_s+'</li></ul></div></div>');
	$("#weatherlist").html("");
	for(i=0;i<weatherlist.length;i++){
		object=weatherlist[i];
		str='<li><ul class="weatherIn_one_columns weatherIn_four_columns"><li class="weather_font">'+object.day_of_week+'</li><li class="small_icon"><img src="image/tools_weather/'+object.icon.split("/")[4].split(".")[0]+'.png" width="70" height="52"/></li>'+
			'<li class="weather_font weather_condition">'+object.condition+'</li><li class="weather_font air_temperature">'+object.low+"&deg~"+object.high+'&deg</li></ul></li>';
		weatherstr+=str;
	}
	if(weatherlist.length>0)
		$("#city").html(searchinput);
	$("#weatherlist").html(weatherstr);
}

function searchbykeyword(){
	var searchinput=$("#text_box").val();
	xhr('POST', 'html5-show/weather', searchinput,setweather,searchinput);
}

$(function(){
	$("#click_seach1").click(function(){
		var searchinput=$("#text_box1").val();
		if(equipment=="phone")
			hidephone();
		xhr('POST', 'html5-show/weather', searchinput,setweather,searchinput);
    })
})

$(function(){
	$("#click_seach").click(function(){
		if(equipment=="phone"){
			showphone();
		}else if(equipment=="pad"){
			clickonpad();	
		}else{
			searchbykeyword();
		}
	})
})

function showphone(){
	$("#light").show();
    $("#fade").show();
}

function hidephone(){
	$("#light").hide();
    $("#fade").hide();
}

function clickonpad(){
	 $("#right_searchbox").hide();
	 $("#light").show();
	 document.getElementById('light').style.right = '-3px';
}

	


